package api

import (
	__ "api-gateway/basic/proto"
	"api-gateway/handler/request"
	"github.com/gin-gonic/gin"
	"google.golang.org/grpc"
	"google.golang.org/grpc/credentials/insecure"
	"log"
	"net/http"
	"path"
)

func ShortPub(c *gin.Context) {
	var req request.ShortPubReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"data": nil,
		})
		return
	}
	conn, err := grpc.NewClient("127.0.0.1:50051", grpc.WithTransportCredentials(insecure.NewCredentials()))
	if err != nil {
		log.Fatalf("did not connect: %v", err)
	}
	defer conn.Close()
	c1 := __.NewSrvClient(conn)
	login, _ := c1.ShortPub(c, &__.ShortPubRequest{
		FormUserId: uint32(c.GetUint("userId")),
		Title:      req.Title,
		Desc:       req.Desc,
		Keysword:   req.Keysword,
		Url:        req.Url,
	})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"data": err.Error(),
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"data": map[string]interface{}{
			"video": login,
		},
	})
}

func VideoPub(c *gin.Context) {

	file, _ := c.FormFile("file")
	if file == nil {
		c.JSON(http.StatusOK, gin.H{
			"data": "上传为空",
		})
		return
	}

	ext := path.Ext(file.Filename)
	if ext != ".mp4" {
		c.JSON(http.StatusOK, gin.H{
			"data": "上传的视频格式错误",
		})
		return
	}
	if file.Size > 50*1024*1024 {
		c.JSON(http.StatusOK, gin.H{
			"data": "上传的视屏过大",
		})
		return
	}
	file.Header.Set("127.0.0.1:8081", "D:\\GOWORK\\zk2\\short_v\\api-gateway\\upload")

}

func ShortList(c *gin.Context) {

	conn, err := grpc.NewClient("127.0.0.1:50051", grpc.WithTransportCredentials(insecure.NewCredentials()))
	if err != nil {
		log.Fatalf("did not connect: %v", err)
	}
	defer conn.Close()
	c1 := __.NewSrvClient(conn)
	login, _ := c1.ShortList(c, &__.ShortListRequest{
		FormUserId: uint32(c.GetUint("userId")),
	})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"data": err.Error(),
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"data": map[string]interface{}{
			"video": login,
		},
	})
}

func ShortInfo(c *gin.Context) {
	var req request.ShortInfoReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"data": nil,
		})
		return
	}
	conn, err := grpc.NewClient("127.0.0.1:50051", grpc.WithTransportCredentials(insecure.NewCredentials()))
	if err != nil {
		log.Fatalf("did not connect: %v", err)
	}
	defer conn.Close()
	c1 := __.NewSrvClient(conn)
	login, _ := c1.ShortInfo(c, &__.ShortInfoRequest{
		Keysword: req.Keysword,
	})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"data": err.Error(),
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"data": map[string]interface{}{
			"video": login,
		},
	})
}
func ShortStatus(c *gin.Context) {
	var req request.ShortStatusReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"data": nil,
		})
		return
	}
	conn, err := grpc.NewClient("127.0.0.1:50051", grpc.WithTransportCredentials(insecure.NewCredentials()))
	if err != nil {
		log.Fatalf("did not connect: %v", err)
	}
	defer conn.Close()
	c1 := __.NewSrvClient(conn)
	login, _ := c1.ShortStatus(c, &__.ShortStatusRequest{
		Id:     int64(req.Id),
		Status: req.Status,
	})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"data": err.Error(),
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"data": map[string]interface{}{
			"video": login,
		},
	})
}
func ShortDel(c *gin.Context) {
	var req request.ShortDelReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"data": nil,
		})
		return
	}
	conn, err := grpc.NewClient("127.0.0.1:50051", grpc.WithTransportCredentials(insecure.NewCredentials()))
	if err != nil {
		log.Fatalf("did not connect: %v", err)
	}
	defer conn.Close()
	c1 := __.NewSrvClient(conn)
	login, _ := c1.ShortDel(c, &__.ShortDelRequest{
		Id: int64(req.Id),
	})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"data": err.Error(),
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"data": map[string]interface{}{
			"video": login,
		},
	})
}
